package com.swf.common.httpClient;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swf.common.base.FwsCharset;

public class HttpClientUtil {
	public static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
	public  String url;
	public  String charst = FwsCharset.defaultCharset;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCharst() {
		return charst;
	}


	public void setCharst(String charst) {
		this.charst = charst;
	}


	public  String httpGet() {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		try {
			CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
			//	System.out.println(httpResponse.getStatusLine());
			HttpEntity entity = httpResponse.getEntity();
			InputStream io = entity.getContent();
			String pageContent = IOUtils.toString(io, charst);
			EntityUtils.consume(entity);
			System.out.println(pageContent);
			return pageContent;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 发送字节数组给指定url
	 * @param url 指定url路径
	 * @param byte_msg  内容字节数组
	 * @return 返回结果
	 */
	public static String send(String url,byte[] byte_msg) {
		String returnContent = null;
		HttpClient httpclient = new DefaultHttpClient(); 
		HttpPost httppost = new  HttpPost(url); 
		ByteArrayEntity entity = new ByteArrayEntity(byte_msg);
		httppost.setEntity(entity);
		HttpResponse response;
		try {
			response = httpclient.execute(httppost);
		HttpEntity returnEntity = response.getEntity();
		if (entity != null) {  
			int statusCode = response.getStatusLine().getStatusCode();
			logger.debug(response.getStatusLine().toString());
			if (statusCode==200){
				returnContent = EntityUtils.toString(returnEntity, "GBK");
				logger.info("返回的内容为  ： " + returnContent);
			}
		}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			httpclient.getConnectionManager().shutdown();
		}
		return returnContent;
	}
	
	
	public static void main(String[] args) {
		HttpClientUtil client = new HttpClientUtil();
		client.setUrl("http://www.taobao.com");
		client.setCharst(FwsCharset.GBK);
		client.httpGet();
	}
}
